<?php
/**
 * 模拟bcs服务
 * @author yaoli<yaoli@pateo.com.cn>
 * @version 1.0  
 */
class gis extends ActionBase1 {
	private $_file= './point.php';
	private $_Send_file= './sendPoint.php';
	private $_uid = 0;
	private $_vin = 0;
	private $_json = NULL;
	function init() {
		if( isset($this->data['request']['data']) ){
			$this->_json = json_decode($this->data['request']['data']);
			$this->_uid = $this->_json->uid;
			$this->_vin = $this->_json->vin;
		}
	}
	
	/**
	 * 动作
	 *
	 * @see AActionBase::action()
	 */
	public function action() {
		
	}
	
	//保存收藏位置点
	public function savePoiPointPost(){
		$json = $this->_json;
		$md5 = md5($this->_uid.$this->_vin);
		if( file_exists($this->_file) ){
			$data = unserialize(file_get_contents($this->_file));
		}else{
			$data = array();
		}
		$total = isset($data[$md5]) ? count($data[$md5])+1 : 1 ;
		$data[md5($this->_uid.$this->_vin)][] = array('uid'=>$this->_uid,'vin'=>$this->_vin,'address'=>$json->address,'telephone'=>$json->telephone,'longitude'=>$json->longitude,'latitude'=>$json->latitude,'name'=>$json->name,'keyword'=>(isset($json->keyword) ? $json->keyword:''),'poiid'=>$total);
		$serialize = serialize($data);
		file_put_contents($this->_file,$serialize);
		$ret = array();
		$ret['code'] = 1;
		$ret['description'] = 'Successful';
		$ret['favourites'] = array('poiid'=>$total);
		echo json_encode($ret);
	}
	// 获取收藏位置点
	public function getPoiPoint(){
		$file = @file_get_contents($this->_file);
		$ret = array();	
		if( $file ){
			$data = unserialize($file);
			if( array_key_exists(md5($this->_uid.$this->_vin),$data) ){
				$data = $data[md5($this->_uid.$this->_vin)];
			}
		}else{
			$data = array();	
		}
		$ret['code'] = 1;
		$ret['description'] = 'Successful';
		$ret['favourites']['item'] = array_values($data);
		echo json_encode($ret);
	}
	// 删除收藏位置点
	public function deletePoiPoint(){
		$keys = $this->_json->poiid;
		$file = @file_get_contents($this->_file);
		if( $file ){
			$data = unserialize($file);
			if( array_key_exists(md5($this->_uid.$this->_vin),$data) ){
				$ret = &$data[md5($this->_uid.$this->_vin)];
				foreach( $ret  as $key=>&$r ){
					if( $r['poiid'] == $keys ){
						unset( $data[md5($this->_uid.$this->_vin)][$key] );	
					}
				}
				$serialize = serialize($data);
				file_put_contents($this->_file,$serialize);
				exit("{\"code\" : 1,\"description\":\"Successful\"}");
			}
		}
		exit("{\"code\" : 0 ,\"description\":\"Failure\"}");
	}
	
	//保存已发送位置点
	public function saveSendPoiPoint(){
		$json = $this->_json;
		if( file_exists($this->_Send_file) ){
			$data = unserialize(file_get_contents($this->_Send_file));
		}else{
			$data = array();
		}
		$sign = md5($this->_uid.$this->_vin);
		$total = isset($data[$sign]) ? count($data[md5($this->_uid.$this->_vin)])+1 : 1 ;
		$data[md5($this->_uid.$this->_vin)][] = array('uid'=>$this->_uid,'vin'=>$this->_vin,'address'=>$json->address,'telephone'=>$json->telephone,'longitude'=>$json->longitude,'latitude'=>$json->latitude,'name'=>$json->name,'sendid'=>$total);
		$serialize = serialize($data);
		file_put_contents($this->_Send_file,$serialize);
		$ret = array();
		$ret['code'] = 1;
		$ret['description'] = 'Successful';
		$ret['favourites'] = array('sendid'=>$total);
		echo json_encode($ret);
	}
	//返回已发送位置点
	public function getSendPoiPoint(){
		$file = @file_get_contents($this->_Send_file);
		$ret = array();	
		if( $file ){
			$data = unserialize($file);
			if( array_key_exists(md5($this->_uid.$this->_vin),$data) ){
				$data = $data[md5($this->_uid.$this->_vin)];
			}
		}else{
			$data = array();	
		}
		$ret['code'] = 1;
		$ret['description'] = 'Successful';
		$ret['favourites']['item'] = array_values($data);
		header('content-type:application/json;charset=utf-8');
		echo json_encode($ret);
	}
	//删除已发送位置点
	public function deleteSendPoiPoint(){
		$keys = $this->_json->sendid;
		$file = @file_get_contents($this->_Send_file);
		if( $file ){
			$data = unserialize($file);
			if( array_key_exists(md5($this->_uid.$this->_vin),$data) ){
				$ret = &$data[md5($this->_uid.$this->_vin)];
				foreach( $ret  as $key=>&$r ){
					if( $r['sendid'] == $keys ){
						unset( $data[md5($this->_uid.$this->_vin)][$key] );	
					}
				}
				$serialize = serialize($data);
				file_put_contents($this->_Send_file,$serialize);
				exit("{\"code\" : 1,\"description\":\"Successful\"}");
			}
		}
		exit("{\"code\" : 0 ,\"description\":\"Failure\"}");
	}
	public function getPrvoince(){
		$data = array();
		$data['code'] = 1;
		$data['timestamp'] = time();
		$data['version'] = "2.0 - 2.0.3185.1545";
		$data['result'] = "true";
		$data['message'] = "Successful";
		$data['city_list']['city'] = array(
			array("adcode"=>"140000",
				"enable"=>"1",
				"latitude"=>"37.857014",
				"longitude"=>"112.549248",
				"name_en"=>"Shanxi Province",
				"name_zh"=>"山西省",
				"pinyin"=>"ShanXiSheng",
				"short"=>"SX",
				"citycode"=>""
			),
			array("adcode"=>"110000",
				"citycode"=>"010",
				"enable"=>"1",
				"latitude"=>"39.904989",
				"longitude"=>"116.405285",
				"name_en"=>"Beijing City",
				"name_zh"=>"北京市",
				"pinyin"=>"BeiJingShi",
				"short"=>"BJ"
			),
			array("adcode"=>"120000",
				"citycode"=>"022",
				"enable"=>"1",
				"latitude"=>"39.125596",
				"longitude"=>"117.190182",
				"name_en"=>"Tianjin City",
				"name_zh"=>"天津市",
				"pinyin"=>"TianJinShi",
				"short"=>"TJ"
			),
			array("adcode"=>"130000",
				"enable"=>"1",
				"latitude"=>"38.045474",
				"longitude"=>"114.502461",
				"name_en"=>"Hebei Province",
				"name_zh"=>"河北省",
				"pinyin"=>"HeBeiSheng",
				"short"=>"HB",
				"citycode"=>""
			),
			array("adcode"=>"220000",
				"enable"=>"1",
				"latitude"=>"43.886841",
				"longitude"=>"125.3245",
				"name_en"=>"Jilin Province",
				"name_zh"=>"吉林省",
				"pinyin"=>"JiLinSheng",
				"short"=>"JL",
				"citycode"=>""
			),
			array("adcode"=>"230000",
				"enable"=>"1",
				"latitude"=>"45.756967",
				"longitude"=>"126.642464",
				"name_en"=>"Heilongjiang Province",
				"name_zh"=>"黑龙江省",
				"pinyin"=>"HeiLongJiangSheng",
				"short"=>"HLJ",
				"citycode"=>""
			),
			array("adcode"=>"150000",
				"enable"=>"1",
				"latitude"=>"40.818311",
				"longitude"=>"111.670801",
				"name_en"=>"Inner Mongolia Autonomous Region",
				"name_zh"=>"内蒙古自治区",
				"pinyin"=>"NeiMengGuZiZhiQu",
				"short"=>"NMGZZ",
				"citycode"=>""
			),
			array("adcode"=>"210000",
				"enable"=>"1",
				"latitude"=>"41.796767",
				"longitude"=>"123.429096",
				"name_en"=>"Liaoning Province",
				"name_zh"=>"辽宁省",
				"pinyin"=>"LiaoNingSheng",
				"short"=>"LN",
				"citycode"=>""
			),
			array("adcode"=>"330000",
				"enable"=>"1",
				"latitude"=>"30.287459",
				"longitude"=>"120.153576",
				"name_en"=>"Zhejiang Province",
				"name_zh"=>"浙江省",
				"pinyin"=>"ZheJiangSheng",
				"short"=>"ZJ",
				"citycode"=>""
			),
			array("adcode"=>"340000",
				"enable"=>"1",
				"latitude"=>"31.86119",
				"longitude"=>"117.283042",
				"name_en"=>"Anhui Province",
				"name_zh"=>"安徽省",
				"pinyin"=>"AnHuiSheng",
				"short"=>"AH",
				"citycode"=>""
			),
			array("adcode"=>"310000",
				"citycode"=>"021",
				"enable"=>"1",
				"latitude"=>"31.231706",
				"longitude"=>"121.472644",
				"name_en"=>"Shanghai City",
				"name_zh"=>"上海市",
				"pinyin"=>"ShangHaiShi",
				"short"=>"SH"
			),
			array("adcode"=>"320000",
				"enable"=>"1",
				"latitude"=>"32.041544",
				"longitude"=>"118.767413",
				"name_en"=>"Jiangsu Province",
				"name_zh"=>"江苏省",
				"pinyin"=>"JiangSuSheng",
				"short"=>"JS",
				"citycode"=>""
			),
			array("adcode"=>"350000",
				"enable"=>"1",
				"latitude"=>"26.075302",
				"longitude"=>"119.306239",
				"name_en"=>"Fujian Province",
				"name_zh"=>"福建省",
				"pinyin"=>"FuJianSheng",
				"short"=>"FJ",
				"citycode"=>""
			),
			array("adcode"=>"410000",
				"enable"=>"1",
				"latitude"=>"34.757975",
				"longitude"=>"113.665412",
				"name_en"=>"Henan Province",
				"name_zh"=>"河南省",
				"pinyin"=>"HeNanSheng",
				"short"=>"HN",
				"citycode"=>""
			),
			array("adcode"=>"360000",
				"enable"=>"1",
				"latitude"=>"28.676493",
				"longitude"=>"115.892151",
				"name_en"=>"Jiangxi Province",
				"name_zh"=>"江西省",
				"pinyin"=>"JiangXiSheng",
				"short"=>"JX",
				"citycode"=>""
			),
			array("adcode"=>"370000",
				"enable"=>"1",
				"latitude"=>"36.675807",
				"longitude"=>"117.000923",
				"name_en"=>"Shandong Province",
				"name_zh"=>"山东省",
				"pinyin"=>"ShanDong Sheng",
				"short"=>"SD",
				"citycode"=>""
			),
			array("adcode"=>"450000",
				"enable"=>"1",
				"latitude"=>"22.82402",
				"longitude"=>"108.320004",
				"name_en"=>"Guangxi Zhuang Autonomous Region",
				"name_zh"=>"广西壮族自治区",
				"pinyin"=>"GuangXi ZhuangZu ZiZhiQu",
				"short"=>"GXZZZZ",
				"citycode"=>""
			),
			array("adcode"=>"420000",
				"enable"=>"1",
				"latitude"=>"30.584355",
				"longitude"=>"114.298572",
				"name_en"=>"Hubei Province",
				"name_zh"=>"湖北省",
				"pinyin"=>"HuBeiSheng",
				"short"=>"HB",
				"citycode"=>""
			),
			array("adcode"=>"430000",
				"enable"=>"1",
				"latitude"=>"28.19409",
				"longitude"=>"112.982279",
				"name_en"=>"Hunan Province",
				"name_zh"=>"湖南省",
				"pinyin"=>"HuNanSheng",
				"short"=>"HN",
				"citycode"=>""
			
			),
			array("adcode"=>"440000",
				"enable"=>"1",
				"latitude"=>"23.125178",
				"longitude"=>"113.280637",
				"name_en"=>"Guangdong Province",
				"name_zh"=>"广东省",
				"pinyin"=>"GuangDongSheng",
				"short"=>"GD",
				"citycode"=>""
			),
			array("adcode"=>"530000",
				"enable"=>"1",
				"latitude"=>"25.040609",
				"longitude"=>"102.712251",
				"name_en"=>"Yunnan Province",
				"name_zh"=>"云南省",
				"pinyin"=>"YunNanSheng",
				"short"=>"YN",
				"citycode"=>""
			),
			array("adcode"=>"520000",
				"enable"=>"1",
				"latitude"=>"26.578343",
				"longitude"=>"106.713478",
				"name_en"=>"Guizhou Province",
				"name_zh"=>"贵州省",
				"pinyin"=>"GuiZhouSheng",
				"short"=>"GZ",
				"citycode"=>""
			),
			array("adcode"=>"460000",
				"enable"=>"1",
				"latitude"=>"20.031971",
				"longitude"=>"110.33119",
				"name_en"=>"Hainan Province",
				"name_zh"=>"海南省",
				"pinyin"=>"HaiNanSheng",
				"short"=>"HN",
				"citycode"=>""
			),
			array("adcode"=>"500000",
				"citycode"=>"023",
				"enable"=>"1",
				"latitude"=>"29.533155",
				"longitude"=>"106.504962",
				"name_en"=>"Chongqing City",
				"name_zh"=>"重庆市",
				"pinyin"=>"ChongQingShi",
				"short"=>"ZQ"
			),
			array("adcode"=>"510000",
				"enable"=>"1",
				"latitude"=>"30.659462",
				"longitude"=>"104.065735",
				"name_en"=>"Sichuan Province",
				"name_zh"=>"四川省",
				"pinyin"=>"SiChuanSheng",
				"short"=>"SC",
				"citycode"=>""
			),
			array("adcode"=>"540000",
				"enable"=>"1",
				"latitude"=>"29.660361",
				"longitude"=>"91.132212",
				"name_en"=>"Tibet Autonomous Region",
				"name_zh"=>"西藏自治区",
				"pinyin"=>"XiZangZiZhiQu",
				"short"=>"XCZZ",
				"citycode"=>""
			),
			array("adcode"=>"620000",
				"enable"=>"1",
				"latitude"=>"36.058039",
				"longitude"=>"103.823557",
				"name_en"=>"Gansu Province",
				"name_zh"=>"甘肃省",
				"pinyin"=>"GanSuSheng",
				"short"=>"GS",
				"citycode"=>""
			),
			array("adcode"=>"630000",
				"enable"=>"1",
				"latitude"=>"36.623178",
				"longitude"=>"101.778916",
				"name_en"=>"Qinghai Province",
				"name_zh"=>"青海省",
				"pinyin"=>"QingHaiSheng",
				"short"=>"QH",
				"citycode"=>""
			),
			array("adcode"=>"610000",
				"enable"=>"1",
				"latitude"=>"34.263161",
				"longitude"=>"108.948024",
				"name_en"=>"Shaanxi Province",
				"name_zh"=>"陕西省",
				"pinyin"=>"ShaanXiSheng",
				"short"=>"SX",
				"citycode"=>""
			),
			array("adcode"=>"640000",
				"enable"=>"1",
				"latitude"=>"38.46637",
				"longitude"=>"106.278179",
				"name_en"=>"Ningxia Hui Autonomous Region",
				"name_zh"=>"宁夏回族自治区",
				"pinyin"=>"NingXia HuiZu ZiZhiQu",
				"short"=>"NXHZZZ",
				"citycode"=>""
			),
			array("adcode"=>"650000",
				"enable"=>"1",
				"latitude"=>"43.792818",
				"longitude"=>"87.617733",
				"name_en"=>"Xinjiang Uygur Autonomous Region",
				"name_zh"=>"新疆维吾尔自治区",
				"pinyin"=>"XinJiang WeiWuEr ZiZhiQu",
				"short"=>"XJWWEZZ",
				"citycode"=>""
			)
		);	
		echo json_encode($data);
	}
	
	public function getCity(){
		$data = array();
		$data['code'] = 1;
		$data['timestamp'] = time();
		$data['version'] = "2.0 - 2.0.3185.1545";
		$data['result'] = "true";
		$data['message'] = "Successful";
		$data['city_list']['city'] = array(
			array("adcode"=>"110000",
				"citycode"=>"010",
				"enable"=>"1",
				"latitude"=>"39.904989",
				"longitude"=>"116.405285",
				"name_en"=>"Beijing City",
				"name_zh"=>"北京市",
				"pinyin"=>"BeiJingShi",
				"short"=>"BJ"
			),
			array("adcode"=>"120000",
				"citycode"=>"022",
				"enable"=>"1",
				"latitude"=>"39.125596",
				"longitude"=>"117.190182",
				"name_en"=>"Tianjin City",
				"name_zh"=>"天津市",
				"pinyin"=>"TianJinShi",
				"short"=>"TJ"
			),
			array("adcode"=>"500000",
				"citycode"=>"023",
				"enable"=>"1",
				"latitude"=>"29.533155",
				"longitude"=>"106.504962",
				"name_en"=>"Chongqing City",
				"name_zh"=>"重庆市",
				"pinyin"=>"ChongQingShi",
				"short"=>"ZQ"
			),
			array("adcode"=>"310000",
				"citycode"=>"021",
				"enable"=>"1",
				"latitude"=>"31.231706",
				"longitude"=>"121.472644",
				"name_en"=>"Shanghai City",
				"name_zh"=>"上海市",
				"pinyin"=>"ShangHaiShi",
				"short"=>"SH"
			),
			array("adcode"=>"110000",
				"citycode"=>"010",
				"enable"=>"1",
				"latitude"=>"39.904989",
				"longitude"=>"116.405285",
				"name_en"=>"Beijing City",
				"name_zh"=>"北京市",
				"pinyin"=>"BeiJingShi",
				"short"=>"BJ"
			),
			array("adcode"=>"120000",
				"citycode"=>"022",
				"enable"=>"1",
				"latitude"=>"39.125596",
				"longitude"=>"117.190182",
				"name_en"=>"Tianjin City",
				"name_zh"=>"天津市",
				"pinyin"=>"TianJinShi",
				"short"=>"TJ"
			),
			array("adcode"=>"500000",
				"citycode"=>"023",
				"enable"=>"1",
				"latitude"=>"29.533155",
				"longitude"=>"106.504962",
				"name_en"=>"Chongqing City",
				"name_zh"=>"重庆市",
				"pinyin"=>"ChongQingShi",
				"short"=>"ZQ"
			),
			array("adcode"=>"310000",
				"citycode"=>"021",
				"enable"=>"1",
				"latitude"=>"31.231706",
				"longitude"=>"121.472644",
				"name_en"=>"Shanghai City",
				"name_zh"=>"上海市",
				"pinyin"=>"ShangHaiShi",
				"short"=>"SH"
			),
			array("adcode"=>"110000",
				"citycode"=>"010",
				"enable"=>"1",
				"latitude"=>"39.904989",
				"longitude"=>"116.405285",
				"name_en"=>"Beijing City",
				"name_zh"=>"北京市",
				"pinyin"=>"BeiJingShi",
				"short"=>"BJ"
			),
			array("adcode"=>"120000",
				"citycode"=>"022",
				"enable"=>"1",
				"latitude"=>"39.125596",
				"longitude"=>"117.190182",
				"name_en"=>"Tianjin City",
				"name_zh"=>"天津市",
				"pinyin"=>"TianJinShi",
				"short"=>"TJ"
			),
			array("adcode"=>"500000",
				"citycode"=>"023",
				"enable"=>"1",
				"latitude"=>"29.533155",
				"longitude"=>"106.504962",
				"name_en"=>"Chongqing City",
				"name_zh"=>"重庆市",
				"pinyin"=>"ChongQingShi",
				"short"=>"ZQ"
			),
			array("adcode"=>"310000",
				"citycode"=>"021",
				"enable"=>"1",
				"latitude"=>"31.231706",
				"longitude"=>"121.472644",
				"name_en"=>"Shanghai City",
				"name_zh"=>"上海市",
				"pinyin"=>"ShangHaiShi",
				"short"=>"SH"
			),
			array("adcode"=>"110000",
				"citycode"=>"010",
				"enable"=>"1",
				"latitude"=>"39.904989",
				"longitude"=>"116.405285",
				"name_en"=>"Beijing City",
				"name_zh"=>"北京市",
				"pinyin"=>"BeiJingShi",
				"short"=>"BJ"
			),
			array("adcode"=>"120000",
				"citycode"=>"022",
				"enable"=>"1",
				"latitude"=>"39.125596",
				"longitude"=>"117.190182",
				"name_en"=>"Tianjin City",
				"name_zh"=>"天津市",
				"pinyin"=>"TianJinShi",
				"short"=>"TJ"
			),
			array("adcode"=>"500000",
				"citycode"=>"023",
				"enable"=>"1",
				"latitude"=>"29.533155",
				"longitude"=>"106.504962",
				"name_en"=>"Chongqing City",
				"name_zh"=>"重庆市",
				"pinyin"=>"ChongQingShi",
				"short"=>"ZQ"
			),
			array("adcode"=>"310000",
				"citycode"=>"021",
				"enable"=>"1",
				"latitude"=>"31.231706",
				"longitude"=>"121.472644",
				"name_en"=>"Shanghai City",
				"name_zh"=>"上海市",
				"pinyin"=>"ShangHaiShi",
				"short"=>"SH"
			)
		);
		echo json_encode($data);
	}
}